﻿Imports System.Net
Imports System.IO
Imports System.Drawing
Imports AUtility
Imports System.Data

Partial Public Class PlayerInfo
    Inherits System.Web.UI.Page

    Private selectStr As String = ""

    Private ReadOnly Property IgracId() As Integer
        Get
            If (Request.QueryString("IgracId") IsNot Nothing) Then
                Return Convert.ToInt32(Request.QueryString("IgracId"))
            Else
                Return 6
            End If
        End Get

    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try
            LoadData()

        Catch ex As Exception
            Response.Write(ex.Message)
        End Try

        'dim igracId = convert
        ZadnjiRezultati(IgracId)

        Try
            hypBack.Text = "Natrag"
            hypBack.NavigateUrl = Request.UrlReferrer.ToString()
            hypBack2.Text = "Natrag"
            hypBack2.NavigateUrl = Request.UrlReferrer.ToString()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub LoadData()
        Dim selectStr As String = String.Format("SELECT * FROM Igrac WHERE IgracId = {0}", IgracId)
        Dim playerRow As DataRow = DataAccess.GetDataRow(GlobalTenisLiga.ConnectionString, selectStr)

        If (Not playerRow.IsNull("Picture")) Then
            'imgPicture.ImageUrl = playerRow("Picture").ToString()
            imgPicture2.ImageUrl = playerRow("Picture").ToString()
            SetImageSize(imgPicture2.ImageUrl)
            imgPicture2.AlternateText = "Klik za veću sliku"
            'imgPicture2.PostBackUrl = playerRow("Picture").ToString()
        End If

        lblName.Text = String.Format("{0} {1}", playerRow("Ime"), playerRow("prezime"))
        lblMail.Text = playerRow("Mail").ToString()
        lblMobitel.Text = playerRow("Mobitel").ToString()

        lblPobjedePorazi.Text = PobjedePorazi(IgracId).ToString
        lblPrijOdigrane.Text = OdigranePrijateljske()

        If (Not IsPostBack) Then
            FillDdl(IgracId)
        End If
    End Sub

    Private Function PobjedePorazi(ByVal igracId As Integer) As String
        Dim out As String = ""

        'Dim win As String = DataAccess.GetString(GlobalTenisLiga.ConnectionString, String.Format("SELECT Count(*) FROM vSusreti Where IgracId = {0} AND Pobjednik = 1", igracId))
        'Dim loose As String = DataAccess.GetString(GlobalTenisLiga.ConnectionString, String.Format("SELECT Count(*) FROM vSusreti Where IgracId = {0} AND Pobjednik = 0", igracId))

        'out = String.Format("{0} / {1}", win, loose)


        selectStr = String.Format("SELECT vIgrac.*, vOdigrane.Odigrane, vPobjede.Pobjede, ROUND( 100*CONVERT(float,Pobjede) /CONVERT(float,Odigrane), 0) AS Omjer  FROM vIgrac JOIN vPobjede ON vIgrac.IgracId = vPobjede.IgracId JOIN vOdigrane ON vIgrac.IgracId = vOdigrane.IgracId WHERE vIgrac.IgracId = {0}", igracId)
        Dim row As DataRow = DataAccess.GetDataRow(GlobalTenisLiga.ConnectionString, selectStr)

        out = String.Format("{0}/{1}  ({2}%)", row("Pobjede"), row("Odigrane"), row("Omjer"))

        Return out
    End Function


    Private Sub ZadnjiRezultati(ByVal igracId As Integer)

        lblScore.Visible = False
        lblScoreValue.Visible = False
        lblSusretiHead.Text = "Zadnji liga susreti"
        Dim selectStr As String = ""
        selectStr = String.Format("SELECT TOP 10 LigaNaziv, ImePrezime, Set1, Set2, Set3, convert(varchar,Datum,104) AS Datum2, Lokacija FROM vSusreti WHERE SusretId IN ( SELECT SusretId FROM vSusreti WHERE IgracId = {0} )  ORDER BY Datum DESC, SusretIgracId", igracId)
       
        lblSusreti.Text = ""
        Dim resultsDT As DataTable = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr)

        For i As Integer = 0 To resultsDT.Rows.Count - 1 Step 2
            lblSusreti.Text = lblSusreti.Text + "<br>" + Result(resultsDT, i)
        Next

    End Sub

    Private Function Result(ByVal resultDT As DataTable, ByVal rowIndex As Integer) As String
        Dim resultTable As String = ""
        If (resultDT.Columns.Contains("LigaNaziv")) Then
            If (resultDT.Rows(rowIndex + 1).IsNull("Set3")) Then
                resultTable = String.Format("<table align=center  width=150><tr><td colspan=4 align=center  bgcolor=#F0F0F0>{0}</td></tr><tr><td colspan=4 align=center>{1} - {2}</td></tr><tr bgcolor=#E0E0E0><td>{3}</td><td>{4}</td><td>{5}</td></tr><tr><td>{6}</td><td>{7}</td><td>{8}</td></tr></table>", resultDT.Rows(rowIndex)("LigaNaziv"), resultDT.Rows(rowIndex)("Datum2"), resultDT.Rows(rowIndex)("Lokacija"), resultDT.Rows(rowIndex)("ImePrezime"), resultDT.Rows(rowIndex)("Set1"), resultDT.Rows(rowIndex)("Set2"), resultDT.Rows(rowIndex + 1)("ImePrezime"), resultDT.Rows(rowIndex + 1)("Set1"), resultDT.Rows(rowIndex + 1)("Set2"))
            Else
                resultTable = String.Format("<table align=center  width=150><tr><td colspan=4 align=center  bgcolor=#F0F0F0>{0}</td></tr><tr><td colspan=4 align=center>{1} - {2}</td></tr><tr bgcolor=#E0E0E0><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr><tr><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td></tr></table>", resultDT.Rows(rowIndex)("LigaNaziv"), resultDT.Rows(rowIndex)("Datum2"), resultDT.Rows(rowIndex)("Lokacija"), resultDT.Rows(rowIndex)("ImePrezime"), resultDT.Rows(rowIndex)("Set1"), resultDT.Rows(rowIndex)("Set2"), resultDT.Rows(rowIndex)("Set3"), resultDT.Rows(rowIndex + 1)("ImePrezime"), resultDT.Rows(rowIndex + 1)("Set1"), resultDT.Rows(rowIndex + 1)("Set2"), resultDT.Rows(rowIndex + 1)("Set3"))
            End If

        Else
            resultTable = String.Format("<table align=cente><tr><td colspan=4 align=center>{0} - {1}</td></tr><tr bgcolor=#E0E0E0><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr><tr><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td></tr></table>", resultDT.Rows(rowIndex)("Datum2"), resultDT.Rows(rowIndex)("Lokacija"), resultDT.Rows(rowIndex)("ImePrezime"), resultDT.Rows(rowIndex)("Set1"), resultDT.Rows(rowIndex)("Set2"), resultDT.Rows(rowIndex)("Set3"), resultDT.Rows(rowIndex + 1)("ImePrezime"), resultDT.Rows(rowIndex + 1)("Set1"), resultDT.Rows(rowIndex + 1)("Set2"), resultDT.Rows(rowIndex + 1)("Set3"))
        End If


            Return resultTable
    End Function

    Private Function OdigranePrijateljske() As String
        selectStr = String.Format("SELECT vOdigranePrijateljske.Odigrane FROM vIgrac JOIN vOdigranePrijateljske on vIgrac.IgracId = vOdigranePrijateljske.IgracId WHERE vIgrac.IgracId = {0}", IgracId)
        Return DataAccess.GetString(GlobalTenisLiga.ConnectionString, selectStr)
    End Function

    Private Sub FillDdl(ByVal igracId As Integer)
        selectStr = String.Format("SELECT DISTINCT ImePrezime, IgracId FROM vSusreti WHERE SusretId IN ( SELECT SusretId FROM vSusreti WHERE SezonaId <> 7 AND IgracId = {0} ) AND IgracId <> {0}", igracId)

        ddlIgraci.DataTextField = "ImePrezime"
        ddlIgraci.DataValueField = "IgracId"
        ddlIgraci.DataSource = DataAccess.AddBlankRow(DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr))
        ddlIgraci.DataBind()

    End Sub

    Private Sub HeadToHead(ByVal igrac1 As Integer, ByVal igrac2 As Integer)

        lblScore.Visible = True
        lblScoreValue.Visible = True
        lblSusretiHead.Text = "Head To Head"
        selectStr = String.Format(" SELECT LigaNaziv, ImePrezime, Set1, Set2, Set3, convert(varchar,Datum,104) AS Datum2, Lokacija FROM vSusreti WHERE SusretId IN ( SELECT SusretId FROM vSusreti WHERE SezonaId <> 7 AND IgracId = {0} AND SusretId IN ( SELECT SusretId FROM vSusreti WHERE SezonaId <> 7 AND IgracId = {1} ) ) ORDER BY Datum DESC", igrac1, igrac2)

        lblSusreti.Text = ""
        Dim resultsDT As DataTable = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr)

        For i As Integer = 0 To resultsDT.Rows.Count - 1 Step 2
            lblSusreti.Text = lblSusreti.Text + "<br>" + Result(resultsDT, i)
        Next

        Dim odigrane As Integer = Convert.ToInt32(resultsDT.Rows.Count / 2)
        selectStr = String.Format(" SELECT COUNT(*) FROM vSusreti WHERE SusretId IN ( SELECT SusretId FROM vSusreti WHERE SezonaId <> 7 AND IgracId = {0} AND SusretId IN ( SELECT SusretId FROM vSusreti WHERE SezonaId <> 7 AND IgracId = {1} ) ) AND Pobjednik = 1 AND IgracId = {0}", igrac1, igrac2)
        Dim pobjede As Integer = DataAccess.GetInt(GlobalTenisLiga.ConnectionString, selectStr)

        lblScoreValue.Text = String.Format("{0} - {1}", pobjede, odigrane - pobjede)

    End Sub


    Protected Sub ddlIgraci_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlIgraci.SelectedIndexChanged
        If ddlIgraci.SelectedItem.Text <> "" Then
            Dim igrac2 As Integer = Convert.ToInt32(ddlIgraci.SelectedValue)
            HeadToHead(IgracId, igrac2)
        Else
            ZadnjiRezultati(IgracId)
        End If
    End Sub

    Protected Sub imgPicture2_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgPicture2.Click
        Response.Redirect(imgPicture2.ImageUrl)
    End Sub


    Private Sub SetImageSize(ByVal imageUrl As String)
        Try
            Dim HttpWReq As HttpWebRequest = CType(WebRequest.Create(imageUrl), HttpWebRequest)

            Dim HttpWResp As HttpWebResponse = CType(HttpWReq.GetResponse(), HttpWebResponse)
            Dim strmReader As StreamReader = New StreamReader(HttpWResp.GetResponseStream())
            Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(strmReader.BaseStream)

            Dim newWidth As Integer = 100
            Dim ratio As Double = img.Width / newWidth

            Dim newHeight As Integer = Convert.ToInt32(img.Height / ratio)

            imgPicture2.Width = newWidth
            imgPicture2.Height = newHeight
        Catch
        End Try

    End Sub
End Class